package com.example.ice.iconfont.icon

import android.content.Context
import android.graphics.Typeface
import android.util.AttributeSet
import android.widget.TextView
import com.example.ice.iconfont.R
import java.util.*

/**
 * Created by ice on 2018/3/30.
 */
class IconTextView : TextView {

    class WrapperIconDrawable(drawable: IconDrawable, isWrapContent: Boolean = true) {
        var drawable: IconDrawable? = null
        var isWrapContent = true

        init {
            this.drawable = drawable
            this.isWrapContent = isWrapContent
        }
    }

    val drawables: Array<WrapperIconDrawable?> = arrayOf(null, null, null, null)

    constructor(context: Context?, attributeSet: AttributeSet) : super(context, attributeSet) {

        context?.let {
            var typedArray = context.obtainStyledAttributes(attributeSet, R.styleable.IconTextView)

            var typefaceString = typedArray.getString(R.styleable.IconTextView_typeface)
            if (typefaceString.isNullOrEmpty()) {
                throw RuntimeException("typeface属性不能为空")
            }

            var fontsClass = context.classLoader.loadClass("${javaClass.`package`.name}.${typefaceString.split(".")[0]}")
            var iconsMap = fontsClass.getDeclaredField("iconsMap").get(null) as TreeMap<String, Char>

            var typeface = Typeface.createFromAsset(context.assets, "font/$typefaceString")


            var drawableLeftString = typedArray.getString(R.styleable.IconTextView_drawableLeft)
            //默认-1 即wrap_content
            var drawableLeftSize = typedArray.getDimension(R.styleable.IconTextView_drawableLeftDp, -1f).toInt()
            var drawableLeftColor = typedArray.getColor(R.styleable.IconTextView_drawableLeftColor, 0)
            if (!drawableLeftString.isNullOrEmpty()) drawables[0] = createWrapperIconDrawable(context, typeface, drawableLeftString, iconsMap, drawableLeftSize, drawableLeftColor)

            var drawableTopString = typedArray.getString(R.styleable.IconTextView_drawableTop)
            var drawableTopSize = typedArray.getDimension(R.styleable.IconTextView_drawableTopDp, -1f).toInt()
            var drawableTopColor = typedArray.getColor(R.styleable.IconTextView_drawableTopColor, 0)
            if (!drawableTopString.isNullOrEmpty()) drawables[1] = createWrapperIconDrawable(context, typeface, drawableTopString, iconsMap, drawableTopSize, drawableTopColor)

            var drawableRightString = typedArray.getString(R.styleable.IconTextView_drawableRight)
            var drawableRightSize = typedArray.getDimension(R.styleable.IconTextView_drawableRightDp, -1f).toInt()
            var drawableRightColor = typedArray.getColor(R.styleable.IconTextView_drawableRightColor, 0)
            if (!drawableRightString.isNullOrEmpty()) drawables[2] = createWrapperIconDrawable(context, typeface, drawableRightString, iconsMap, drawableRightSize, drawableRightColor)

            var drawableBottomString = typedArray.getString(R.styleable.IconTextView_drawableBottom)
            var drawableBottomSize = typedArray.getDimension(R.styleable.IconTextView_drawableBottomDp, -1f).toInt()
            var drawableBottomColor = typedArray.getColor(R.styleable.IconTextView_drawableBottomColor, 0)
            if (!drawableBottomString.isNullOrEmpty()) drawables[3] = createWrapperIconDrawable(context, typeface, drawableBottomString, iconsMap, drawableBottomSize, drawableBottomColor)

            typedArray.recycle()

            setCompoundDrawables(drawables[0]?.drawable, drawables[1]?.drawable, drawables[2]?.drawable, drawables[3]?.drawable)
        }
    }

    fun createWrapperIconDrawable(context: Context, typeface: Typeface, icon: String, iconsMap: TreeMap<String, Char>, size: Int, color: Int) =
            WrapperIconDrawable(IconDrawable(context)
                    .typeface(typeface)
                    .icon(icon)
                    .iconsMap(iconsMap)
                    .color(color)
                    .sizePx(size)
                    .run(), size == -1)

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        for (drawable in drawables) {
            drawable?.let {
                if (drawable.isWrapContent) {
                    drawable.drawable?.sizePx(measuredHeight)
                    drawable.drawable?.run()
                }
            }
        }
        setCompoundDrawables(drawables[0]?.drawable, drawables[1]?.drawable, drawables[2]?.drawable, drawables[3]?.drawable)
    }
}